
let fs = require("fs");
let formatJSON = require("json-format");
let xml2js = require('xml2js');
let lang = require("./Localization.js");
let parseString = xml2js.parseString;
let xmlPath = "F:/st/steamapps/common/7 Days To Die/Data/Config/recipes.xml";
let itemxmlPath = "F:/st/steamapps/common/7 Days To Die/Data/Config/items.xml";






let xml = fs.readFileSync(xmlPath).toString();
let xmlItem = fs.readFileSync(itemxmlPath).toString();

let recipes = [];
let allItems = {};

parseString(xml, function (err, result) {
	result.recipes.recipe.forEach(item => {
		let rp = {...item.$}
		if(item.ingredient) {
			rp.ingredient = {}
			item.ingredient.map(item2 => rp.ingredient[n2c(item2.$.name)] = item2.$.count)
		}
        rp.name = n2c(rp.name)
		recipes.push(rp);
	});

	parseString(xmlItem, function (err, result2) {
		result2.items.item.forEach(item => {
			let iteminfo = {
				property: {},
				effect_group: item.effect_group
			}
			if(item.property) {
				item.property.forEach(pro => {
					if(pro.$.name) {
						iteminfo.property[pro.$.name] = pro.$.value
					} else if (pro.$.class) {
						if(pro.property) {
							iteminfo.property[pro.$.class] = pro.property.map(v => v.$.name + ":" + v.$.value).join(" ");
						}
					}
				}) 
			}
			allItems[n2c(item.$.name)] = iteminfo
		})
	});
});

fs.writeFileSync("./items.js", "let a = " + formatJSON(allItems));
fs.writeFileSync("./recipes.js", "let a = " + formatJSON(recipes));




function n2c(name) {
    if(lang[name]) {
        return lang[name].cn
    }    
    return name
}